from model.University import University
from model.UniversityMajorRankMean import UniversityMajorRankMean


# 批量处理院校名称后面带数字，学校名称后面带有（精准专项）的名字，还是显示（精准脱贫）
def to_do():
    universityMajorRankMean = UniversityMajorRankMean()
    year = 2022
    data = universityMajorRankMean.getList(year)
    for index, row in enumerate(data):
        print(index, row)
        if row['uname']:
            update = dict()
            update['id'] = row['id']
            uname = row['uname']
            uname = uname.replace(" ", "")
            # 去掉右边的数字
            if uname[-1].isdigit():
                d = uname[len(uname.rstrip('0123456789')):]
                uname = uname.split(d)[0]
            # 去掉字符串所有空格 院校名称是否有 精准脱贫 ，有则改成 精准专项
            update['uname'] = uname.replace("精准脱贫", "精准专项")
            universityMajorRankMean.toUpdateName(update)


def test():
    a = '中央民族大学（少数民族语言）465'
    print(a[-1].isdigit())
    d = a[len(a.rstrip('0123456789')):]
    print(a.split(d)[0])
    print(a[len(a.rstrip('0123456789')):])


# 以university_2为准，批量补全院校地址
def update_address():
    university = University()
    universityMajorRankMean = UniversityMajorRankMean()
    year = 2022
    data = universityMajorRankMean.getList(year)
    for index, row in enumerate(data):
        #print(index, row)
        # 从院校库获取基础数据
        one = university.getOneByCode(row['code'])
        if one:
            # print('one', one)
            update = dict()
            update2 = dict()
            if one['province_code'] and one['province']:
                update['id'] = row['id']
                update['province_code'] = one['province_code']
                update['province'] = one['province']
                print(update)
                universityMajorRankMean.toUpdateAddress(update)
            if one['city_code'] and one['city']:
                update2['id'] = row['id']
                update2['city_code'] = one['city_code']
                update2['city'] = one['city']
                print(update2)
                universityMajorRankMean.toUpdateAddress2(update2)


if __name__ == "__main__":
    update_address()